package com.sogoodsoft.web.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Customer;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.crm.CustomerService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

/**
 * @author Administrator
 *
 */
@Results({
	@Result(name = "selectCustomer", location = "/WEB-INF/content/crm/selectCustomer.jsp"),
	@Result(name = "MYCUSTOMER", location = "mycustomer.jsp") 
	})
public class CustomerAction extends CrudActionSupport < Customer > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private CustomerService customerManager;

    @Autowired
    private AccountManager accountManager;

    @Autowired
    private SysClassService sysService;

    private Page < Customer > page = new Page < Customer >(WebConstants.PAGE_SIZE);

    private Long id;

    private Customer entity;
    
    private Long userid;
    private String customers;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public Customer getModel() {
        return entity;
    }

    public Page < Customer > getPage() {
        return page;
    }

    public void setPage(Page < Customer > page) {
        this.page = page;
    }

    public Customer getEntity() {
        return entity;
    }

    public void setEntity(Customer entity) {
        this.entity = entity;
    }

    @Override
    public String list() throws Exception {
        List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.DESC);
        }
        page = customerManager.findPage(page, filters);
        return SUCCESS;
    }

    public String myCustomer()throws Exception{
    	 List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
    	  User user = InfosysSecurityUtil.getCurrentUser();
    	 PropertyFilter prop = new PropertyFilter("EQL_belong",String.valueOf(user.getId()));
    	 filters.add(prop);
    	 if (!page.isOrderBySetted()) {
             page.setOrderBy("id");
             page.setOrder(Page.DESC);
         }
         page = customerManager.findPage(page, filters);
    	return "MYCUSTOMER";
    }
    public String selectCustomer() throws Exception {
        list();
        return "selectCustomer";
    }
    public String dispatch()throws Exception{
    	logger.debug(userid.toString());
    	logger.debug(customers);
    	customerManager.dispatch(userid,customers);
    	Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
    	return null;
    }
    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        
        Long typeid = entity.getCustomertype();
        if (typeid != null) {
            SysClass type = sysService.get(typeid);
            entity.setType(type);

        }

        Long levelid = entity.getStar();
        if (levelid != null) {
            SysClass star = sysService.get(levelid);
            entity.setCustomerStar(star);
        }

        customerManager.save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public String delete() throws Exception {
        entity = customerManager.get(id);
        customerManager.delete(entity);
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = customerManager.get(id);
        } else {
            entity = new Customer();
        }
    }

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public String getCustomers() {
		return customers;
	}

	public void setCustomers(String customers) {
		this.customers = customers;
	}



}
